<?php
/**
 * Created by PhpStorm.
 * User: jobs
 * Date: 2017/6/22 0022
 * Time: 下午 5:36
 */

class JobsController extends Admin {

    public $jobs;
    public $sima;
    public $linkage;
    public function __construct() {
        parent::__construct();
        $this->linkage = $this->model('linkage');
        $this->sima = $this->model('zhou_sima');
        $this->jobs = $this->model('zhou_jobs');
    }

    public function jobs_listAction() {
        if ($this->post('submit_del')) {
            if ($this->post('del_')=='') {
                $this->adminMsg('请选择你要删除的记录!', url('admin/jobs/jobs_list'), 2, 1, 2);
            }else{
                foreach ($_POST as $var=>$value) {
                    if (strpos($var, 'del_')!==false) {
                        $id = (int)str_replace('del_', '', $var);
                        $this->del2Action($id, 1);
                    }
                }
                $this->adminMsg('多条记录删除成功', url('admin/jobs/jobs_list'), 3, 1, 1);
            }
        }
        $page = (int)$this->get('page') ? (int)$this->get('page') : 1;
        if($this->post('page')){
            $page = (int)$this->post('page');
        }
        $typeid = $this->post('typeid') ? $this->post('typeid') : $this->get('typeid');
        $pagesize = isset($this->site['SITE_ADMIN_PAGESIZE']) && $this->site['SITE_ADMIN_PAGESIZE'] ? $this->site['SITE_ADMIN_PAGESIZE'] : 8;

        $where = "1=1";
        //搜索
        if($typeid){
            $where .= " and typeid=".$typeid;
        }
        $urlparam = array('page' => '{page}','typeid'=>$typeid);
        $order = " jobs_id desc";
        $limit = ($page-1)*$pagesize.",$pagesize";
        $list =  $this->jobs->get_admin_list($where,$order ,$limit); //列表
        $total = $this->jobs->get_admin_list($where,$order ,$limit,true); //统计数量
        $pagelist = $this->instance('pagelist');	//加载分页类
        $pagelist->loadconfig();
        $pagelist = $pagelist->total($total)->url(url('admin/jobs/jobs_list', $urlparam))->num($pagesize)->page($page)->output();
        $province = $this->linkage->get_all_list();//地区

        $xinzi = $this->sima->get_cate_all('zhou_salary', '1=1 order by salary_ord asc');
        $xinzi_arr = tran_one($xinzi,'salary_id','salary_name');
        $work = $this->sima->get_cate_all('zhou_work_type', '1=1 order by work_id asc');
        $work_arr = tran_one($work,'work_id','work_name');
        $work_time = $this->sima->get_cate_all('zhou_work_time', 'time_show = 1 and 1 order by time_ord asc');
        $work2 = tran_one($work_time,'time_id','time_name');
        $this->view->assign(array(
            'work2'=>$work2,
            'work_arr'=>$work_arr,
            'xinzi_arr'=>$xinzi_arr,
            'province'=>$province,
            'page' => $page,
            'typeid' => $typeid,
            'list' => $list,
            'total' => $total,
            'pagelist' => $pagelist,
        ));
        $this->view->display('admin/jobs_list');
    }

    public function editAction(){
        $id = (int)$this->get('id');
        $list = $this->jobs->get_admin_show($id);
        if($this->post('submit')){
            $data = $this->post('data');
            $this->jobs->set($id,$data);
            $this->adminMsg(lang('success'), url('admin/jobs/edit',array('id'=>$id)), 3, 1, 1);
        }
        $province = $this->linkage->get_all_list();//地区
        $this->view->assign(array(
            'province'=>$province,
            'list' => $list,
        ));
        $this->view->display('admin/jobs_add');
    }

//    //直接删除内容
//    public function delAction($id=0, $all=0) {
//        $id = (int)$this->get('id');
//        $this->adminMsg('已删除', url('admin/jobs/index'), 3, 1, 1);die;
//        if (!auth::check($this->roleid, 'caidou-del', 'admin')) {
//            $this->adminMsg(lang('a-com-0', array('1'=>'jobs_moban', '2'=>'del')));
//        }
//        $all = $all ? $all : $this->get('all');
//        $id = $id ? $id : (int)$this->get('id');
//        $this->jobs->del($id);
//        $all or $this->adminMsg($this->getCacheCode('caidou') . lang('success'), url('admin/jobs_moban/index'), 3, 1, 1);
//    }

    //修改删除的状态
    public function update_delAction() {
        $id = (int)$this->get('id');
        $data = $this->jobs->get_admin_show($id);
        if($data){
            $data['del']=1;
            $this->jobs->del($id);
            $this->adminMsg(lang('success'), url('admin/jobs_moban/index'), 2, 1, 1);
        }else{
            $this->adminMsg(lang('要删除的内容不存在'), url('admin/jobs_moban/index'));
        }
    }

    //修改某一/多 字段
    public function statusAction() {
        $id = (int)$this->get('id');
        $list = $this->jobs->get_admin_show($id);
        if($list){
            $data['status']=2;
            $this->jobs->set($id,$data);
            $this->adminMsg(lang('success'), url('admin/jobs_moban/index'), 2, 1, 1);
        }else{
            $this->adminMsg(lang('要审核的内容不存在'), url('admin/jobs_moban/index'));
        }
    }
    //iFrames
    public function HuaAction(){
        $list = array();
        $list[0] = 1;
        $list[1] = 2;
        $list[2] = 2;
        $list[3] = 2;
        $this->view->assign(array(
            'list' => $list,
        ));
        $this->view->display('admin/jobs_iFrames');
    }

    public function delAction(){
        $id = $this->safe('id');
        $this->jobs->del($id);
        $this->adminMsg('已删除', url('admin/jobs/jobs_list'), 3, 1, 1);
    }

    public function jobs_daoruAction(){
        $table = 'zhou_jobs';
        if ($_POST) {//1覆盖，
            if ($_POST['fugai'] == 1) {
                $this->jobs->db_truncate($table);
            }
            $xls_url = $_POST['start_xls'];
            $file_types = explode ( ".", $xls_url );
            $excel_type = array('xls','csv','xlsx');
            //判断是不是excel文件
            if (!in_array(strtolower(end($file_types)),$excel_type)){
                $this->adminMsg("不是Excel文件，重新上传",$_SERVER['HTTP_REFERER']);
            }
            $arr = excelToArray($xls_url,$file_types[1]);
            if(is_array($arr)){
                unset($arr[1]);
                foreach($arr as $key => $value){
                    $data['jobs_name'] = $value[0];
                    if($value[1]=='招聘'){
                        $data['typeid'] = 2;
                    }else{
                        $data['typeid'] = 1;
                    }
                    if($value[2]=='长期工'){
                        $data['jobs_work_type'] = 1;
                    }else{
                        $data['jobs_work_type'] = 2;
                    }
                    if($value[3]>0){
                        $data['jobs_age'] = $value[3];
                    }else{
                        $data['jobs_age'] = '';
                    }
                    $data['jobs_position'] = $value[4];
                    if($value[5]!=''){
                        $data['jobs_sex'] = $value[5];
                    }else{
                        $data['jobs_sex'] = '';
                    }
                    $data['jobs_area'] = $value[6];
                    $money = substr($value[7],0,1);
                    $data['jobs_salary'] = $money;
                    $time = substr($value[8],0,1);
                    $data['jobs_work_time'] = $time;
                    $data['jobs_tel'] = $value[9];
                    $data['jobs_addtime'] = time();
                    if($value[10]!=''){
                        $data['jobs_nikename'] = $value[10];
                    }else{
                        $data['jobs_nikename'] = '';
                    }
                    if($value[11]!=''){
                        $data['jobs_content'] = $value[11];
                    }else{
                        $data['jobs_content'] = '';
                    }
                    $this->jobs->set('',$data);
                }

            }
            $this->adminMsg(lang('success'), url('admin/jobs/jobs_list'), 3, 1, 1);die();
        }
        $this->view->display('admin/jobs_daoru_edit');
    }

    public function downAction(){
        $filename = "jobs.xls";
        $file = fopen(FCPATH.'uploadfiles/model/'.$filename,"r");
        //
        Header("Content-type:application/octet-stream");
        Header("Accept-Ranges:bytes");
        header("Content-Type:application/msexcel");
        Header("Accept-Length:".filesize(FCPATH.'uploadfiles/model/'.$filename));
        Header("Content-Disposition:attachment;filename=".$filename);
        echo
        fread($file,filesize(FCPATH.'uploadfiles/model/'.$filename));
        fclose($file);
        $this->adminMsg(lang('success'), url('sima/zhou_position/area_list'),2, 1,1);
    }

}